﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework;

namespace AGrandVenture
{
    class MeleeWeaponItem : InventoryItem
    {
        #region Enums
        public enum DamageTypes
        {
            Crushing, Slashing, Piercing, Cleaving, Poison, Electrocution, Freezing, Psychic, Spiritual, Starvation, Disease, Magic
        }
        #endregion
        #region Declarations
        private static Random rnd = new Random();
        private DamageTypes damageType;
        private double baseMinDmg,
                       baseMaxDmg,
                       baseRange,
                       baseAttackSpeed,
                       baseCriticalStrike = 0;

        public static string[] MeleeWeaponNames = new String[] { "Sword","Mace","Club","Axe", "Dagger", "Great Axe", "Great Sword", 
                                                                      "Claymore", "Halberd", "Spear", "Staff", "Saber", "Scimitar", 
                                                                      "Bastard Sword" };
        #endregion

        #region Properties

        public DamageTypes DamageType
        {
            get { return damageType; }
            set { damageType = value; }
        }
        public double MinBaseDmg
        {
            get { return baseMinDmg; }
            set { baseMinDmg = value; }
        }
        public double MaxBaseDmg
        {
            get { return baseMaxDmg; }
            set { baseMaxDmg = value; }
        }
        public double BaseRange
        {
            get { return baseRange; }
            set { baseRange = value; }
        }
        public double BaseAttackSpeed
        {
            get { return baseAttackSpeed; }
            set { baseAttackSpeed = value; }
        }
        public double BaseCriticalStrike
        {
            get { return baseCriticalStrike; }
            set { baseCriticalStrike = value; }
        }
        public double TotalMinDmg
        {
            get { return baseMinDmg + BonusMinDmg; }
        }
        public double TotalMaxDmg
        {
            get { return baseMaxDmg + BonusMaxDmg; }
        }
        public double TotalRange
        {
            get { return baseRange + BonusRange; }
        }
        public double TotalAttackSpeed
        {
            get { return baseAttackSpeed + BonusAttackSpeed; }
        }
        public double TotalCritRate
        {
            get { return baseCriticalStrike + BonusCriticalStrike; }
        }

        #endregion

        #region Constructors
        public MeleeWeaponItem(UInt64 seedValue)
        {
            string prefix = DataManager.GreyItemPrefix[SeedUtility.getSeedPair(4,5,seedValue)%(DataManager.GreyItemPrefix.Count - 1)];
            string suffix = DataManager.GreyItemSuffix[SeedUtility.getSeedPair(6,7,seedValue)%(DataManager.GreyItemSuffix.Count - 1)];
            int level = SeedUtility.getSeedLevel(seedValue);
            Level = level;
            Texture2D texture = Game1.equipmentSheet;
            Rectangle initialFrame = new Rectangle(0, 0, 48, 48);
            string name = DataManager.WeaponNames[SeedUtility.getSeedPair(2,3, seedValue)%(DataManager.WeaponNames.Count - 1)];
            EquipSlot = InventoryItem.EquipmentSlot.Primary;
            Name = prefix + " " + name + " of " + suffix;
            setBaseStatsByName(name, level);

            texture = Game1.equipmentSheet;
            initialFrame = new Rectangle(0, 0, 48, 48);
            Base = new Sprite(
                Vector2.Zero,
                texture,
                initialFrame,
                Vector2.Zero,
                0.1f);

            if (Rarity == ItemRarity.Trash)
            {
                clearBonusStats();//Gray items don't get innate item-type stats, just Base stats and name bonuses
                setBonusesByGrayPrefix(prefix, level);
                setBonusesByGraySuffix(suffix, level);
            }
            else if (Rarity == ItemRarity.Common)
            {
                //Common items get the same bonuses as Gray items, but they keep their innate item-type bonuses
                setBonusesByGrayPrefix(prefix, level);
                setBonusesByGraySuffix(suffix, level);
            }
            Type = ItemType.Weapon;
        }
        #endregion

        private void setBaseStatsByName(string name, int level)
        {
            switch(name)
            {
                case "Sword":
                    damageType = DamageTypes.Slashing;
                    baseMinDmg = 1;
                    baseMaxDmg = 4;
                    baseRange = 1;
                    baseAttackSpeed = 1;
                    baseCriticalStrike = 1;
                    break;
                case "Katana":
                    damageType = DamageTypes.Slashing;
                    baseMinDmg = 2;
                    baseMaxDmg = 5;
                    baseRange = 1;
                    baseAttackSpeed = 1;
                    baseCriticalStrike = 1;
                    break;
                case "Mace":
                    damageType = DamageTypes.Crushing;
                    baseMinDmg = 2;
                    baseMaxDmg = 4;
                    baseRange = 0.75;
                    baseAttackSpeed = 1.25;
                    baseCriticalStrike = 0.75;
                    break;
                case "Club":
                    damageType = DamageTypes.Crushing;
                    baseMinDmg = 2;
                    baseMaxDmg = 3;
                    baseRange = 0.75;
                    baseAttackSpeed = 1.5;
                    baseCriticalStrike = 1;
                    break;
                case "Axe":
                    damageType = DamageTypes.Slashing;
                    baseMinDmg = 3;
                    baseMaxDmg = 5;
                    baseRange = 0.75;
                    baseAttackSpeed = 1;
                    baseCriticalStrike = 1.4;
                    break;
                case "Dagger":
                    damageType = DamageTypes.Piercing;
                    baseMinDmg = 2;
                    baseMaxDmg = 5;
                    baseRange = 0.5;
                    baseAttackSpeed = 2;
                    baseCriticalStrike = 2;
                    break;
                case "Great Axe":
                    damageType = DamageTypes.Cleaving;
                    baseMinDmg = 4;
                    baseMaxDmg = 8;
                    baseRange = 1.5;
                    baseAttackSpeed = .5;
                    baseCriticalStrike = 1;
                    break;
                case "Great Sword":
                    damageType = DamageTypes.Cleaving;
                    baseMinDmg = 3;
                    baseMaxDmg = 8;
                    baseRange = 1.8;
                    baseAttackSpeed = .4;
                    baseCriticalStrike = 2;
                    break;
                case "Claymore":
                    damageType = DamageTypes.Cleaving;
                    baseMinDmg = 2;
                    baseMaxDmg = 8;
                    baseRange = 1.25;
                    baseAttackSpeed = .75;
                    baseCriticalStrike = 1;
                    break;
                case "Halberd":
                    damageType = DamageTypes.Piercing|DamageTypes.Slashing;
                    baseMinDmg = 2;
                    baseMaxDmg = 8;
                    baseRange = 2;
                    baseAttackSpeed = .75;
                    baseCriticalStrike = 1;
                    break;
                case "Spear":
                    damageType = DamageTypes.Piercing;
                    baseMinDmg = 2;
                    baseMaxDmg = 6;
                    baseRange = 2.5;
                    baseAttackSpeed = .75;
                    baseCriticalStrike = 1.25;
                    break;
                case "Staff":
                    damageType = DamageTypes.Crushing;
                    baseMinDmg = 1;
                    baseMaxDmg = 4;
                    baseRange = 2.25;
                    baseAttackSpeed = 1.25;
                    baseCriticalStrike = .75;
                    break;
                case "Saber":
                    damageType = DamageTypes.Slashing;
                    baseMinDmg = 2;
                    baseMaxDmg = 3;
                    baseRange = 1.25;
                    baseAttackSpeed = 1.25;
                    baseCriticalStrike = 1;
                    break;
                case "Scimitar":
                    damageType = DamageTypes.Slashing;
                    baseMinDmg = 2;
                    baseMaxDmg = 6;
                    baseRange = .75;
                    baseAttackSpeed = 1.5;
                    baseCriticalStrike = 1.25;
                    break;
                case "Bastard Sword":
                    baseMinDmg = 4;
                    baseMaxDmg = 6;
                    baseRange = 1.25;
                    baseAttackSpeed = 1;
                    baseCriticalStrike = 1;
                    break;
            }
            //Apply item level modifier
            double levelModifier = 1 + (double)(level / 99);
            baseMinDmg *= levelModifier;
            baseMaxDmg *= levelModifier;
            baseRange *= levelModifier;
            baseAttackSpeed *= levelModifier;
            baseCriticalStrike *= levelModifier;
        }
    }
}
